/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "iot_cloud.h"
#include "iot_profile.h"
#include "cJSON.h"
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/**
 * @brief we make the data to a json string and send it to the iot cloud engine
 *
 */
int IotProfile_Report(int temperature, int humidity, int lux, int CO2, int watertemp, int TDS, float PH)
{
    int ret = -1;
    cJSON *root;
    char *jsonString;
    IotProfileService service;
    IotProfileKV kvTemperature;
    IotProfileKV kvHumidity;
    IotProfileKV kvLux;
    IotProfileKV kvCO2;
    IotProfileKV kvWatertemp;
    IotProfileKV kvTDS;
    IotProfileKV kvPH;

    /* package the data */
    service.eventTime = NULL;
    service.serviceID = "SmartAquaponics";
    service.propertyLst = &kvTemperature;
    service.nxt = NULL;

    kvTemperature.key = "Temperature";
    kvTemperature.value = &temperature;
    kvTemperature.type = IOT_PROFILE_KEY_DATATYPE_INT;
    kvTemperature.nxt = &kvHumidity;

    kvHumidity.key = "Humidity";
    kvHumidity.value = &humidity;
    kvHumidity.type = IOT_PROFILE_KEY_DATATYPE_INT;
    kvHumidity.nxt = &kvLux;

    kvLux.key = "Lux";
    kvLux.value = &lux;
    kvLux.type = IOT_PROFILE_KEY_DATATYPE_INT;
    kvLux.nxt = &kvCO2;

    kvCO2.key = "CO2";
    kvCO2.value = &CO2;
    kvCO2.type = IOT_PROFILE_KEY_DATATYPE_INT;
    kvCO2.nxt = &kvWatertemp;

    kvWatertemp.key = "Watertemp";
    kvWatertemp.value = &watertemp;
    kvWatertemp.type = IOT_PROFILE_KEY_DATATYPE_INT;
    kvWatertemp.nxt = &kvTDS;

    kvTDS.key = "TDS";
    kvTDS.value = &TDS;
    kvTDS.type = IOT_PROFILE_KEY_DATATYPE_INT;
    kvTDS.nxt = kvPH;

    kvPH.key = "PH";
    kvPH.value = &PH;
    kvPH.type = IOT_PROFILE_KEY_DATATYPE_FLOAT;
    kvPH.nxt = NULL;

    jsonString = IoTProfilePackage(&service);
    if (NULL != jsonString)
    {
        RaiseLog(LOG_LEVEL_INFO, "jsonString:%s", jsonString);

        ret = CLOUD_ReportMsg(jsonString);
        free(jsonString);
    }
    else
    {
        RaiseLog(LOG_LEVEL_ERR, "format the report message error");
    }
    return ret;
}
int CLOUD_CommandCallBack(const char *jsonString) {}
